$(function () {
  localStorage.removeItem('x-token');
  let form = layui.form;
  let layer = layui.layer;
  $('.layui-form').submit(e => {
    let login_name = $('input[name="username"]').val().trim();
    let password = $('input[name="password"]').val().trim();
    e.preventDefault();
    axios({
      method: 'post',
      url: '/index/login',
      headers: {
        token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6MTM2LCJ1c2VybmFtZSI6IjExMSIsIm1vYmlsZSI6IjE4ODY4ODE2MTg1Iiwic2V4IjowLCJwYXNzd29yZCI6IiIsImljb24iOiJ1cGxvYWQvaW1hZ2VzLzIwMjIwNzExLzE2NTc1NDM0MzA2MjYuanBnIiwibm90ZXMiOiLlsLHmmK_niZvpgLznmoTnrqHnkIblkZgiLCJiaXJ0aGRheSI6IjIwMDAtMTEtMTIiLCJhZGRyZXNzIjoi5rKz5YyX55yBIOWUkOWxseW4giDot6_ljZfljLoiLCJkZWZhdWx0X2RhdGEiOiIxIn0sImlhdCI6MTY1NzU4Mzc5OX0.FCeSpLd_25dwL7s0m8mcPVZrXQPHO2VOu7IZHSshdG4',
      },
      data: {
        login_name,
        password,
      },
    }).then(res => {
      console.log(res);
      if (res.status !== 200) return false;
      if (res.data.errmsg === '账号不正确' || res.data.errmsg === '密码不正确') {
        return layer.msg(res.data.errmsg);
      }
      layer.msg('账号密码正确,登陆成功');
      localStorage.setItem('x-token', res.data.data.token);

      setTimeout(() => {
        location.href = './index.html';
      }, 500);
    });
  });
});
